package com.yeyks.common.appcommon.result;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 分页数据封装类
 * @author Administrator
 */
public class CommonPage<T> {
    private Integer pageNum;
    private Integer pageSize;
    private Long totalPage;
    private Long total;
    private List<T> list;

    /**
     *  将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(Integer pageNum, Integer lastId, List<T> list) {
        CommonPage<T> result = new CommonPage<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        //解决分页重复数据的问题
        long total = 0L;
        if(lastId != null){
            total = pageInfo.getTotal() + (pageNum-1) * pageInfo.getPageSize();
        }else{
            total = pageInfo.getTotal();
        }
        result.setTotal(total);
        if(total != 0L){
            long totalPage = total / pageInfo.getPageSize();
            result.setTotalPage((total % pageInfo.getPageSize()) == 0 ? totalPage : totalPage +1);
        }
        result.setPageNum(pageNum);
        result.setPageSize(pageInfo.getPageSize());
        result.setList(pageInfo.getList());
        return result;
    }

    public CommonPage() {
         this.pageNum = 0;
         this.pageSize = 0;
         this.total = 0L;
         this.totalPage = 0L;
         this.list = Lists.newArrayList();
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
